package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.MarketCommentMapper;
import com.cskaoyan.catmall.model.bo.CommentBo;
import com.cskaoyan.catmall.model.bo.CommentReplyBo;
import com.cskaoyan.catmall.model.pojo.MarketComment;
import com.cskaoyan.catmall.model.pojo.MarketCommentExample;
import com.cskaoyan.catmall.model.vo.CommentVo;
import com.cskaoyan.catmall.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 评论
 * 创建日期: 2022/01/09 16:55
 *
 * @author ghr
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;

    /**
     * @param
     * @param param
     * @return com.cskaoyan.catmall.model.vo.CommentVo
     * @description 评论列表
     * @author ghr
     * @date 2022/1/9 5:26 下午
     */
    @Override
    public CommentVo selectComment(CommentBo commentBo) {
        Integer page = commentBo.getPage();
        Integer limit = commentBo.getLimit();
        String sort = commentBo.getSort();
        String order = commentBo.getOrder();
        Integer userId = commentBo.getUserId();
        Integer valueId = commentBo.getValueId();
        PageHelper.startPage(page, limit);

        MarketCommentExample marketCommentExample = new MarketCommentExample();
        StringBuffer clause = new StringBuffer();
        clause.append(sort).append(" ").append(order);
        marketCommentExample.setOrderByClause(clause + "");
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null) {
            criteria.andValueIdEqualTo(valueId);
        }
        criteria.andTypeEqualTo((byte) 0);
        List<MarketComment> marketComments = marketCommentMapper.selectByExample(marketCommentExample);

        PageInfo<MarketComment> marketCommentPageInfo = new PageInfo<>(marketComments);
        long total = marketCommentPageInfo.getTotal();
        int pages = marketCommentPageInfo.getPages();
        return CommentVo.commentVo(total, pages, limit, page, marketComments);
    }


    /**
     * @param
     * @param marketComment
     * @return void
     * @description 删除评论
     * @author ghr
     * @date 2022/1/9 5:26 下午
     */
    @Override
    public void deleteComment(MarketComment marketComment) {
        marketComment.setDeleted(true);
        marketCommentMapper.updateByPrimaryKey(marketComment);
    }

    /**
     * @description 筛选重复回复
     * @author ghr
     * @date 2022/1/10 12:50 下午
     * @param
     * @param commentId
     * @return int
     */
    @Override
    public int selectDuplicateComment(Integer commentId) {
        int num = marketCommentMapper.selectDuplicateComment(commentId);
        return num;
    }

    /**
     * @description 回复订单
     * @author ghr
     * @date 2022/1/10 12:55 下午
     * @param
     * @param commentReplyBo
     * @return void
     */
    @Override
    public void updateComment(CommentReplyBo commentReplyBo) {
        Date date = new Date();
        MarketComment marketComment = new MarketComment();
        marketComment.setAdminContent(commentReplyBo.getContent());
        marketComment.setUpdateTime(date);
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        criteria.andIdEqualTo(commentReplyBo.getCommentId());
        marketCommentMapper.updateByExampleSelective(marketComment,marketCommentExample);
    }
}